<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Validator;
use Cookie;

class LoginController extends Controller
{
    /**
     * @return string
     */
    public function admin()
    {
        return $admin = new Admin();
    }

    /**
     * 管理员注册视图
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getSignup()
    {
        /*初始化视图变量赋值为0*/
        return view('admin/signup',['msg' => 0]);
    }

    /**
     * 注册管理员操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postSignup(Request $request)
    {
        $result = $this->admin()->signup($request);
        if($result['status'])
            return redirect('back/login')->with(['msg' => $result['msg']]);
        else
            return back()->with(['msg' => $result['msg']]);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getLogin()
    {
        return view('admin/login');
    }

    /**
     * post方法登录后台要验证密码
     * @param Request $request
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function postIndex(Request $request)
    {
        /*密码和用户名正确才允许登录*/
        $result = $this->admin()->login($request);
        if($result['status'])
            return view('admin/index',['msg' => $result['msg']]);
        else
            /*密码或用户名错误重定向到登录视图*/
            return back()->with(['msg' => $result['msg']]);
    }
}
